package com.example.demo.service.impl;

import com.example.demo.domain.vo.ProcessInstanceVO;
import com.example.demo.domain.vo.TaskVO;
import com.example.demo.service.HuiQianService;
import org.activiti.bpmn.model.Task;
import org.activiti.engine.HistoryService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.TaskQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: zsg
 * @Date: 2018/07/11 14:44
 */
@Service
public class HuiQianServiceImpl implements HuiQianService {

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private HistoryService historyService;

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private IdentityService identityService;

    @Override
    public  List<ProcessInstanceVO> startProcess(String processKey, String userId, String assigneeList) {

        Map<String, Object> processInstVar = new HashMap<String, Object>();
        //记录流程发起人
        identityService.setAuthenticatedUserId(userId);
        //必须是List
        processInstVar.put("assigneeList", Arrays.asList(assigneeList.split(",")));
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processKey, processInstVar);

        List<org.activiti.engine.task.Task> list = taskService.createTaskQuery().processInstanceId(processInstance.getId()).list();

        List<ProcessInstanceVO> processInstanceVOList = new ArrayList<>();

        list.forEach(task -> {
            ProcessInstanceVO processInstanceVO = new ProcessInstanceVO();
            processInstanceVO.setDeploymentId(processInstance.getDeploymentId());
            processInstanceVO.setId(processInstance.getId());
            processInstanceVO.setProcessDefinitionId(processInstance.getProcessDefinitionId());
            processInstanceVO.setName(processInstance.getName());
            processInstanceVO.setActivityId(processInstance.getActivityId());
            processInstanceVO.setProcessDefinitionName(processInstance.getProcessDefinitionName());
            processInstanceVO.setTaskId(task.getId());
            processInstanceVO.setTaskName(task.getName());
            processInstanceVOList.add(processInstanceVO);
        });

        return processInstanceVOList;
    }

    @Override
    public void compeletTask(String taskId) {
        taskService.complete(taskId);

    }

    @Override
    public List<TaskVO> getMytask(String userId) {
        List<org.activiti.engine.task.Task> list = taskService.createTaskQuery().taskCandidateUser(userId).list();
        List<TaskVO> taskVoList = new ArrayList<>();
        list.forEach(task -> {
            TaskVO taskVo = new TaskVO();
            taskVo.setAssignee(task.getAssignee());
            taskVo.setExecutionId(task.getExecutionId());
            taskVo.setId(task.getId());
            taskVo.setName(task.getName());
            taskVo.setProcessDefinitionId(task.getProcessDefinitionId());
            taskVo.setProcessInstanceId(task.getProcessInstanceId());
            taskVoList.add(taskVo);
        });
        return taskVoList;
    }
}
